package com.sky.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CotegoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class CategoryServiceImpl implements CotegoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Override
    public void save(CategoryDTO categoryDTO) {
        Category category = new Category();
        //对象属性拷贝
        BeanUtils.copyProperties(categoryDTO, category);
        //设置状态为启用
        category.setStatus(StatusConstant.ENABLE);
        categoryMapper.insert(category);
    }
    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
       PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        Page<Category> page = categoryMapper.pageQuery(categoryPageQueryDTO);
        return  new PageResult(page.getTotal(), page.getResult());
    }
    @Override
    public void startOrStop(Integer status, Long id) {
        /*构建 Category 对象：
        使用 Category.builder() 创建一个 Category 构建器。
        设置 id 和 status 属性。
        调用 build 方法生成 category对象
         */
         Category category = Category.builder()
                            .id(id)
                            .status(status)
                            .build();
         categoryMapper.update(category);
    }

    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        //对象属性拷贝
        BeanUtils.copyProperties(categoryDTO, category);
        categoryMapper.update(category);
    }

    @Override
    public void deleteById(List<Long> ids) {
        //1.起售的菜品不能删除
        for (Long id : ids) {
            Category category = categoryMapper.deleteById(id);
            Integer status = category.getStatus();
            if(status == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
            }
        }
        //2.被套餐关联的菜品不能删除
        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishId(ids);
        for (Long setmealId : setmealIds) {
            //判断套餐是否起售
            Setmeal setmeal = setmealMapper.getById(setmealId);
            if (setmeal.getStatus()==StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
        }
    }

    @Override
    public List<Category> list(Integer type) {
        List<Category> category = categoryMapper.list(type);
        return category;

    }
}
